package com.example.ams.repository;

import com.example.ams.entity.ClockRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 打卡记录数据访问层
 * Clock Record Repository
 */
@Repository
public interface ClockRecordRepository extends JpaRepository<ClockRecord, Long> {

    /**
     * 根据员工ID查询打卡记录
     */
    List<ClockRecord> findByEmployeeIdOrderByEventTimeDesc(Long employeeId);

    /**
     * 根据员工ID和时间范围查询
     */
    @Query("SELECT cr FROM ClockRecord cr WHERE cr.employeeId = :employeeId " +
           "AND cr.eventTime >= :startTime AND cr.eventTime <= :endTime " +
           "ORDER BY cr.eventTime DESC")
    List<ClockRecord> findByEmployeeIdAndTimeRange(
            @Param("employeeId") Long employeeId,
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime);

    /**
     * 根据时间范围查询所有记录
     */
    @Query("SELECT cr FROM ClockRecord cr WHERE cr.eventTime >= :startTime " +
           "AND cr.eventTime <= :endTime ORDER BY cr.eventTime DESC")
    List<ClockRecord> findByTimeRange(
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime);

    /**
     * 根据数据来源查询
     */
    List<ClockRecord> findBySourceOrderByEventTimeDesc(ClockRecord.Source source);
}

